package com.rj2002.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rj2002.dao.GoodsMapper;
import com.rj2002.dao.GoodstypeMapper;
import com.rj2002.dao.OrdersMapper;
import com.rj2002.pojo.Goods;
import com.rj2002.pojo.Goodstype;
import com.rj2002.pojo.Orders;
import com.rj2002.pojo.User;
import com.rj2002.util.FileUploadUtils;
import com.rj2002.util.ResultUtil;

@Controller
@RequestMapping("/sale")
public class SaleDataCtrl {
	
	@Autowired
	private GoodstypeMapper typeMapper;
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private OrdersMapper ordersMapper;
	
	@RequestMapping("/uploadFile")
	@ResponseBody
	public ResultUtil uploadFile(MultipartFile file) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			String imgurl = FileUploadUtils.upload(file, "saleGoods");
			resultUtil.setCode(0);
			resultUtil.setData(imgurl);
		} catch (Exception e) {
			resultUtil.setCode(500);
			resultUtil.setData("error:"+e.getMessage());
		}
		return resultUtil;
	}
	
	@RequestMapping("/goodList")
	@ResponseBody
	public ResultUtil goodList(Integer page, Integer limit,Goods goods, HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("account");
		goods.setUserid(user.getId());
		
		PageHelper.startPage(page, limit);
		PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsMapper.selectAllUser(goods));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	
	@RequestMapping("/deleteGoods")
	@ResponseBody
	public ResultUtil deleteGoods(int id) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			goodsMapper.deleteByPrimaryKey(id);
			resultUtil.setCode(0);
			resultUtil.setData("删除成功");
		} catch (Exception e) {
			resultUtil.setCode(500);
			resultUtil.setData("error:"+e.getMessage());
		}
		return resultUtil;
	}
	
	@RequestMapping("/getTypes")
	@ResponseBody
	public ResultUtil getTypes() {
		ResultUtil resultUtil = new ResultUtil();
		try {
			List<Goodstype> list = typeMapper.getTypeList(new Goodstype());
			resultUtil.setCode(0);
			resultUtil.setData(list);
		} catch (Exception e) {
			resultUtil.setCode(500);
			resultUtil.setData("error:"+e.getMessage());
		}
		return resultUtil;
	}
	
	@RequestMapping("/addGoods")
	@ResponseBody
	public ResultUtil addGoods(Goods goods,HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("account");
		goods.setUserid(user.getId());
		goods.setFlag(1);
		System.err.println(goods);
		ResultUtil resultUtil = new ResultUtil();
		try {
			int i = goodsMapper.insertSelective(goods);
			if(i == 1) {
				resultUtil.setCode(0);
				resultUtil.setData("添加成功");
			}else {
				resultUtil.setCode(201);
				resultUtil.setData("添加失败");
			}
		} catch (Exception e) {
			resultUtil.setCode(500);
			resultUtil.setData("error:"+e.getMessage());
		}
		return resultUtil;
	}
	
	@RequestMapping("/editGoods")
	@ResponseBody
	public ResultUtil editGoods(Goods goods,HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("account");
		goods.setUserid(user.getId());
		goods.setFlag(1);
		
		//goods.setImgurl( goods.getImgurl()==""?null:goods.getImgurl() );
		
		System.err.println(goods);
		ResultUtil resultUtil = new ResultUtil();
		//修改数据库信息
		try {
			int i = goodsMapper.updateByPrimaryKeySelective(goods);
			if(i == 1) {
				resultUtil.setCode(0);
				resultUtil.setData("ok:修改成功！");
			}
		} catch (Exception e) {
			resultUtil.setCode(500);
			resultUtil.setData("error:"+e.getMessage());
		}
		
		return resultUtil;
	}
	
	@RequestMapping("/getOrdersList")
	@ResponseBody
	public ResultUtil getOrdersList(Integer page, Integer limit,Orders orders, HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("account");
		orders.setUserid(user.getId());
		
		PageHelper.startPage(page, limit);
		PageInfo<Orders> pageInfo = new PageInfo<Orders>(ordersMapper.selectAll2(orders));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	
	@RequestMapping("/updateGoodsFlag")
	@ResponseBody
	public ResultUtil updateGoodsFlag(Orders goods, HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			ordersMapper.updateByPrimaryKeySelective(goods);
		} catch (Exception e) {
			resultUtil.setData("更新失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("更新成功");
		resultUtil.setCode(0);
		return resultUtil;
	}
}
